<template>
  <div class="login-container">
    <!-- 左侧宣传区域 -->
    <div class="login-left">
      <div class="welcome-content">
        <div class="logo">
          <div class="logo-icon">
            <div class="heart"></div>
            <div class="hands"></div>
          </div>
          <h1>智慧养老中心</h1>
        </div>
        
        <div class="slogan">
          <h2>用心守护 · 温暖相伴</h2>
          <p>让每一位长者都感受到家的温暖</p>
        </div>

        <div class="features">
          <div class="feature-item">
            <div class="feature-icon">🏠</div>
            <div class="feature-text">
              <h3>温馨家园</h3>
              <p>舒适宜居的养老环境</p>
            </div>
          </div>
          <div class="feature-item">
            <div class="feature-icon">👨‍⚕️</div>
            <div class="feature-text">
              <h3>专业照护</h3>
              <p>24小时贴心服务</p>
            </div>
          </div>
          <div class="feature-item">
            <div class="feature-icon">❤️</div>
            <div class="feature-text">
              <h3>爱心陪伴</h3>
              <p>让关爱无处不在</p>
            </div>
          </div>
        </div>

        <div class="quote">
          <p>"在这里，每一天都是充满温暖和关爱的美好时光"</p>
        </div>
      </div>
    </div>

    <!-- 右侧登录表单 -->
    <div class="login-right">
      <div class="login-form-container">
        <div class="back-to-home" @click="$router.push('/')">
          <el-icon><ArrowLeft /></el-icon>
          返回首页
        </div>
        
        <div class="login-form">
          <h2>欢迎登录</h2>
          <p class="login-subtitle">智慧养老中心管理系统</p>
          
          <el-form :model="form" label-width="0" class="custom-form">
            <el-form-item>
              <el-input 
                v-model="form.loginName" 
                placeholder="请输入用户名"
                size="large"
                :prefix-icon="User"
              />
            </el-form-item>
            <el-form-item>
              <el-input 
                type="password" 
                v-model="form.password" 
                placeholder="请输入密码"
                size="large"
                :prefix-icon="Lock"
                show-password
              />
            </el-form-item>
            <el-form-item>
              <el-select 
                v-model="form.role" 
                placeholder="请选择角色"
                size="large"
                style="width: 100%"
              >
                <el-option label="管理员" value="admin"/>
                <el-option label="护工" value="caregiver"/>
                <el-option label="前台" value="reception"/>
                <el-option label="家属" value="family"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button 
                type="primary" 
                @click="doLogin" 
                size="large"
                class="login-btn"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>

          <div class="register-link">
            <span>还没有账号？</span>
            <el-link type="primary" @click="$router.push('/register')">立即注册</el-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { login } from '../api/auth.js'
import { ElMessage } from 'element-plus'
import { User, Lock, ArrowLeft } from '@element-plus/icons-vue'

const router = useRouter()

const form = reactive({
  loginName: '',
  password: '',
  role: 'admin'
})

const doLogin = async () => {
  try {
    console.log('开始登录请求，表单数据:', form)
    console.log('请求URL应该是: /api/auth/login')
    const res = await login(form)
    console.log('登录响应:', res)
    
    if (res.code === 200) {
      ElMessage.success('登录成功')

      const englishRole = form.role // ✅ 直接用工单里选的角色
      
      // 确保用户信息格式正确 - axios拦截器已经返回了response.data，所以res就是data
      const userData = res.data || {}
      const userInfo = {
        ...userData,
        name: userData.name || userData.loginName || userData.phone || form.loginName || '用户', // 保证有值
        role: englishRole
      }
      
      console.log('存储的用户信息:', userInfo)
      localStorage.setItem('userInfo', JSON.stringify(userInfo))

      // 根据角色跳转到对应的首页
      const roleRoutes = {
        'caregiver': '/dashboard/caregiver-dashboard',
        'admin': '/dashboard/admin-dashboard',
        'reception': '/dashboard/reception-dashboard',
        'family': '/dashboard/family-dashboard'
      }
      
      const targetRoute = roleRoutes[englishRole] || '/dashboard/admin-dashboard'
      
      console.log('登录成功，准备跳转到:', targetRoute)
      console.log('用户信息已存储:', userInfo)
      
      // 使用replace而不是push，避免浏览器历史记录问题
      router.replace(targetRoute).then(() => {
        console.log('跳转成功')
      }).catch(err => {
        console.error('跳转失败', err)
        // 如果跳转失败，尝试直接导航到dashboard
        router.replace('/dashboard').then(() => {
          console.log('备用跳转成功')
        }).catch(fallbackErr => {
          console.error('备用跳转也失败', fallbackErr)
          ElMessage.error('页面跳转失败，请刷新页面重试')
        })
      })

    } else {
      ElMessage.error(res.msg || '登录失败')
    }
  } catch (error) {
    console.error('登录请求失败:', error)
    ElMessage.error('网络错误，请检查服务器连接')
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.9) 0%, rgba(118, 75, 162, 0.9) 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grid" width="10" height="10" patternUnits="userSpaceOnUse"><path d="M 10 0 L 0 0 0 10" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="0.5"/></pattern></defs><rect width="100" height="100" fill="url(%23grid)"/></svg>');
  opacity: 0.3;
}

.welcome-content {
  max-width: 500px;
  text-align: center;
  z-index: 1;
}

.logo {
  margin-bottom: 40px;
}

.logo-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  position: relative;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.heart {
  width: 30px;
  height: 30px;
  background: #ff6b6b;
  transform: rotate(45deg);
  position: relative;
  animation: heartbeat 2s ease-in-out infinite;
}

.heart::before,
.heart::after {
  content: '';
  width: 30px;
  height: 30px;
  background: #ff6b6b;
  border-radius: 50%;
  position: absolute;
}

.heart::before {
  top: -15px;
  left: 0;
}

.heart::after {
  top: 0;
  left: -15px;
}

.hands {
  position: absolute;
  width: 40px;
  height: 20px;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 10px;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
}

.logo h1 {
  font-size: 2.5rem;
  margin: 0;
  font-weight: 300;
  letter-spacing: 2px;
}

.slogan {
  margin-bottom: 50px;
}

.slogan h2 {
  font-size: 2rem;
  margin-bottom: 10px;
  font-weight: 400;
}

.slogan p {
  font-size: 1.2rem;
  opacity: 0.9;
}

.features {
  margin-bottom: 40px;
}

.feature-item {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
  padding: 15px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  backdrop-filter: blur(10px);
}

.feature-icon {
  font-size: 2rem;
  margin-right: 15px;
}

.feature-text h3 {
  margin: 0 0 5px 0;
  font-size: 1.1rem;
}

.feature-text p {
  margin: 0;
  opacity: 0.8;
  font-size: 0.9rem;
}

.quote {
  font-style: italic;
  opacity: 0.8;
  border-left: 3px solid rgba(255, 255, 255, 0.5);
  padding-left: 15px;
}

.quote p {
  margin: 0;
  font-size: 1.1rem;
}

.login-right {
  flex: 1;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.login-form-container {
  max-width: 400px;
  width: 100%;
}

.back-to-home {
  display: flex;
  align-items: center;
  color: #667eea;
  cursor: pointer;
  margin-bottom: 30px;
  font-size: 0.9rem;
  transition: color 0.3s;
}

.back-to-home:hover {
  color: #764ba2;
}

.login-form {
  text-align: center;
}

.login-form h2 {
  font-size: 2rem;
  margin-bottom: 10px;
  color: #333;
  font-weight: 400;
}

.login-subtitle {
  color: #666;
  margin-bottom: 40px;
  font-size: 1rem;
}

.custom-form {
  text-align: left;
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 1.1rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 8px;
  margin-top: 10px;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.register-link {
  margin-top: 20px;
  text-align: center;
  color: #666;
}

.register-link span {
  margin-right: 8px;
}

@keyframes heartbeat {
  0%, 100% {
    transform: rotate(45deg) scale(1);
  }
  50% {
    transform: rotate(45deg) scale(1.1);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }
  
  .login-left {
    flex: none;
    height: 40vh;
    padding: 20px;
  }
  
  .login-right {
    flex: none;
    height: 60vh;
    padding: 20px;
  }
  
  .logo h1 {
    font-size: 2rem;
  }
  
  .slogan h2 {
    font-size: 1.5rem;
  }
}
</style>
